/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.mq;

import com.medusa.gruul.account.api.model.IntegraChangelDto;
import com.medusa.gruul.integral.api.constant.IntegralQueueEnum;
import com.medusa.gruul.integral.api.constant.IntegralQueueNameConstant;
import com.medusa.gruul.integral.api.model.IntegralOrderDTO;
import com.medusa.gruul.integral.service.IIntegralDetailService;
import com.medusa.gruul.order.api.constant.OrderConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.AmqpTemplate;

/**
 * @Description:
 * @Author: xiaoq
 * @Date : 2019/12/19 11:36
 */

@Slf4j
@Component
public class Sender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private IIntegralDetailService integralDetailService;

    /**
     * 积分增减操作
     *
     * @param integraChangelDto
     */
    public void sendIntegralDeductionMessage(IntegraChangelDto integraChangelDto) {
        log.info("receive message:" + integraChangelDto.toString());
        convertAndSend(integraChangelDto, IntegralQueueEnum.QUEUE_INTEGRAL_DEDUCTION);
    }

    private void convertAndSend(Object message, IntegralQueueEnum queue) {
        amqpTemplate.convertAndSend(queue.getExchange(), queue.getRouteKey(), message);
    }


    public void delayIntegralOrder(IntegralOrderDTO IntegralOrder, long seconds) {
        amqpTemplate.convertAndSend(
                OrderConstant.DELAY_EXCHANGE_NAME,
                IntegralQueueNameConstant.INTEGRAL_PAY_FAIL,
                IntegralOrder,
                (message) -> {
                    message.getMessageProperties().setHeader(MessageProperties.X_DELAY,seconds*1000);
                    return message;
                }
        );
    }


}
